﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class HocKyDetailView : Form
    {
        public event EventHandler SaveChanged;
        public event EventHandler DeleteChanged;
        private HocKy _hocKy;

        private BindingList<HocKy> bindingListHocKy;
        private BindingList<NamHoc> bindingListNamHoc;

        public HocKyDetailView()
        {
            InitializeComponent();
        }
        public HocKyDetailView(HocKy hocKy)
        {
            InitializeComponent();

            _hocKy = hocKy;
            bindingListHocKy = new BindingList<HocKy>();
            bindingListHocKy.Add(_hocKy);

            bindingListNamHoc = new BindingList<NamHoc>(DataAccessObject.NamHoc.GetListNamHoc());

            string[] listFieldName = new string[]{"TenNamHoc","TuNam","DenNam"};
            string[] listColumnName = new string[]{"Tên Năm Học","Từ Nam","Đến Nam"};
            AppGridView.InitGridView(gridViewNamHoc, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gridViewNamHoc);
            AppGridView.ShowField(gridViewNamHoc, listFieldName, listColumnName);

            bindingSourceHocKy.DataSource = bindingListHocKy;
            bindingSourceNamHoc.DataSource = bindingListNamHoc;
            if (hocKy != null && hocKy.NamHoc != null)
                gridNamHoc.EditValue = hocKy.NamHoc.Oid;
            layoutControl1.Refresh();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dxValidationProvider1.Validate())
                Luu();
        }

        private void LuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dxValidationProvider1.Validate())
                Luu();
            this.Close();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OnDeleteChange();
            this.Close();
        }

        #region User Functions
        private void Luu()
        {
            if (_hocKy.Oid == -1)
            {
                if (DataAccessObject.HocKy.Insert(_hocKy) > 0)
                    OnSaveChange();
            }
            else
                DataAccessObject.HocKy.Update(_hocKy);
        }

        void OnSaveChange()
        {
            if (SaveChanged != null)
                SaveChanged(this, EventArgs.Empty);
        }
        void OnDeleteChange()
        {
            if (DeleteChanged != null)
                DeleteChanged(this, EventArgs.Empty);
        }

        #endregion

        private void HocKyDetailView_Load(object sender, EventArgs e)
        {
            //dateEdit1.EditValue = dateEdit2.EditValue = DateTime.Today;
        }

        private void gridNamHoc_EditValueChanged(object sender, EventArgs e)
        {

            if (gridViewNamHoc.GetFocusedRow() != null)
                _hocKy.NamHoc = gridViewNamHoc.GetFocusedRow() as NamHoc;
        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            _hocKy.TuNgay = Convert.ToDateTime(dateTuNgay.EditValue);
        }

        private void dateDenNgay_EditValueChanged(object sender, EventArgs e)
        {
            _hocKy.DenNgay = Convert.ToDateTime(dateDenNgay.EditValue);
        }

    }
}
